package com.sg.web;

import com.github.pagehelper.PageInfo;
import com.sg.bean.*;
import com.sg.service.MessageService;
import com.sg.service.UsedService;
import com.sg.service.UsedStateService;
import com.sg.service.UserService;
import com.sg.util.NewFile;
import com.sg.vo.MessageVO;
import com.sg.vo.UsedVO;
import com.sg.vo.UserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author zl
 * 二手闲置品控制器
 */
@Controller
public class UsedController {

    Logger logger = Logger.getLogger(UsedController.class);

    @Autowired
    UsedService usedService;

    @Autowired
    MessageService messageService;

    @Autowired
    UserService userService;

    @Autowired
    UsedStateService usedStateService;

    @RequestMapping("usedlist")
    public String usedList(Model model,String pageNum,@RequestParam(defaultValue = "4") int pageSize){
        logger.info(111);
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";

        }

        List<UsedVO> list = usedService.findAllPage(Integer.parseInt(pageNum),pageSize);

        PageInfo pageInfo = new PageInfo(list);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        page.setServletUrl("/usedlist.action");
        model.addAttribute("page",page);
        return "usedlist";
    }

    @RequestMapping("toaddused")
    public String toaddused(User user, Model model){
        List<UserVO> list = userService.findAll();
        model.addAttribute(list);
        logger.info("coming toaddused...");
        return "user/addused";
    }

    @RequestMapping("/addused")
    public String addused(Used used,@RequestParam("file1") CommonsMultipartFile file1) throws IOException {
        logger.info("coming addused...");
        if (file1 != null && file1.getSize() > 0) {
            File newFile = NewFile.getFile(file1);
            logger.info("logo"+newFile);
            file1.transferTo(newFile);//上传到目标位置
            used.setUsedImg(NewFile.getNewFileNameAnd(newFile.getName()));
            logger.info(used.getUsedImg());
        }
        usedService.add(used);
        return "redirect:infousedlist.action";
    }

    @RequestMapping("admin/useddelete")
    public String useddelete(int[] arrays){
        logger.info("coming useddelete...");
        int i= messageService.deleteByUsedId(arrays);
        usedService.delete(arrays);
        logger.info(i);
        return "redirect:/usedlist.action";
    }

    @RequestMapping("/useddelete")
    public String useruseddelete(int[] arrays){
        logger.info("coming useddelete...");
        int i= messageService.deleteByUsedId(arrays);
        usedService.delete(arrays);
        logger.info(i);
        return "redirect:infousedlist.action";
    }



    @RequestMapping("/tousedupdate")
    public String tousedupdate(){
        logger.info("coming tousedupdate...");
        return "admin/usedupdate";
    }


    @RequestMapping("usedupdate")
    public String usedupdate(Used used){
        logger.info("coming usedupdate");
        usedService.update(used);

        return "redirect:../admin/usedlist.action";
    }


    @RequestMapping("messagelist")
    public String messagelist(Model model,String pageNum,@RequestParam(defaultValue = "4") int pageSize){
        logger.info("coming messagelist");
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        List<MessageVO> list = messageService.findAll(Integer.parseInt(pageNum),pageSize);

        PageInfo pageInfo = new PageInfo(list);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        page.setServletUrl("/messagelist.action");
        model.addAttribute("page",page);
        return "messagelist";
    }

    @RequestMapping("admin/messagedelete")
    public String messageDelete(int[] arrays){
        logger.info("coming messagedelete...");
        logger.info(Arrays.toString(arrays));
        boolean flag = messageService.delete(arrays);
        logger.info(flag);
        return "redirect:../messagelist.action";
    }

    @RequestMapping("messagedelete")
    public String usermessageDelete(int[] arrays){
        logger.info("coming usermessagedelete...");
        logger.info(Arrays.toString(arrays));
        boolean flag = messageService.delete(arrays);
        logger.info(flag);
        return "redirect:infomessagelist.action";
    }


    @RequestMapping("/toaddmessage")
    public String toaddmessage(Model model,Used used){
        logger.info("coming toaddmessage...");
        List<UsedVO> list = usedService.findAll();
        model.addAttribute("list",list);
        return "user/addmessage";
    }


    @RequestMapping("addmessage")
    public String addmessage(Message message){
        logger.info(message);
        logger.info("coming addmessage...");
        messageService.add(message);
        return "redirect:infomessagelist.action";
    }



    @RequestMapping("infomessagelist")
    public String infomessagelist(HttpSession session, Model model, String pageNum, @RequestParam(defaultValue = "4") int pageSize){
        logger.info("coming messagelist");
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        Object obj = session.getAttribute("createUser");

        UserVO userVO = (UserVO)obj;
        List<MessageVO> list = messageService.findByuIdPage(userVO.getuId(),Integer.parseInt(pageNum),pageSize);
        PageInfo pageInfo = new PageInfo(list);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        page.setServletUrl("/infomessagelist.action");
        model.addAttribute("page",page);
        return "user/infomessagelist";
    }



    @RequestMapping("infousedlist")
    public String infousedlist(HttpSession session,Model model,String pageNum,@RequestParam(defaultValue = "4") int pageSize){
        logger.info(111);
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";

        }
        Object obj = session.getAttribute("createUser");
        UserVO userVO = (UserVO)obj;
        List<UsedVO> list = usedService.findByUIdPage(userVO.getuId(),Integer.parseInt(pageNum),pageSize);
        PageInfo pageInfo = new PageInfo(list);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        page.setServletUrl("/infousedlist.action");
        model.addAttribute("page",page);
        return "user/infousedlist";
    }


}
